//
//  OutputCell.swift
//  DSP
//
//  Created by tucici on 2024/5/15.
//

import UIKit
import TCCKit
@objc protocol OutputCellDelegate {
    func select(output:OutputModel, index:IndexPath)
    func changeName(output:OutputModel, index:IndexPath)
}

class OutputCell: UITableViewCell {
    private var line : UIView!
    private var title : UILabel!
    private var collectionView : UICollectionView!
    var delegate : OutputCellDelegate?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        backgroundColor = .clear
        selectionStyle = .none
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        title = UILabel()
        title.text = "Output Select"
        title.textHexColor = "#000000"
        title.bgHexColor = "#C0C0C0"
        title.font = TCCKit.font(18.0,.medium)
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .horizontal
        flowlayout.minimumLineSpacing = k_w(20)
        flowlayout.minimumInteritemSpacing = k_h(10)
        flowlayout.sectionInset = UIEdgeInsets(top: 4.k_h, left: 20.k_w, bottom: 6.k_h, right: 20.k_w)
        collectionView = UICollectionView(frame:.zero,collectionViewLayout:flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(OutputItem.self, forCellWithReuseIdentifier:OutputItem.ID())
        
        contentView.addSubview(line)
        contentView.addSubview(title)
        contentView.addSubview(collectionView)
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.centerY.equalTo(title.snp.centerY)
        }
        
        title.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(title.snp.bottom)
            make.height.equalTo(0)
        }
    }
    
    
    var array = [OutputModel](){
        didSet{
            guard let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else{return}
            let w = itemWidth(flowlayout)
            flowlayout.itemSize = CGSize(width: w, height: floor(w / 4.0 * 2.5))
            let h = collectionH(flowlayout, array)
            collectionView.reloadData()
            collectionView.snp.updateConstraints { make in
                make.height.equalTo(h)
            }
        }
    }
}

//MARK: - UICollectionViewDataSource
extension OutputCell:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:OutputItem.ID(), for:indexPath) as! OutputItem
        let model = array[indexPath.row]
        cell.model = model
        cell.borderHexColor = model.enable ? HEX_BLACK : HEX_BORDER_DISABLE
        cell.longpress.minimumPressDuration = 0.5
        cell.longPressBlock = {[weak self] in
            let vc = ChangeNameController()
            vc.text = model.name
            self?.vc.present(vc, animated: false)
            vc.block = {[weak self] name in
                model.name = name
                self?.delegate?.changeName(output: model, index: indexPath)
            }
        }
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension OutputCell:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        let model = array[indexPath.row]
        if model.enable == false || model.select {return}
        delegate?.select(output: model, index: indexPath)
    }
}

